package framework.bean.factory;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class ObjectFactory {

    private static Map<String,Object> objects=new HashMap<String,Object>();

    static {

        BufferedReader br=null;
        try{

            br=new BufferedReader(
                    new InputStreamReader(
                            ObjectFactory.class
                                         .getClassLoader()
                                         .getResourceAsStream("objects.cfg")));

            String s=null;

            while ((s=br.readLine())!=null){
                String[] entry=s.split("=");
                objects.put(entry[0],Class.forName(entry[1]).newInstance());
            }



        }catch (Exception e){
            e.printStackTrace();
            throw new ExceptionInInitializerError("ObjectFactory初始化失败");
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static Object getObject(String key){
        return objects.get(key);
    }



}
